﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ControlModule.Models;
using ControlModule.Security;

namespace ControlModule.Daos
{
    public class ControlQuestionDao
    {
        public static string GetTextQuestionByQuestionId(int questionId)
        {
            string textQuestion = "";
            using (CmDbEntities db = new CmDbEntities())
            {
                ControlQuestion controlQuestion = (
                    from cq in db.ControlQuestion
                    where cq.Id == questionId
                    select cq).FirstOrDefault();
                if (controlQuestion != null)
                    textQuestion = controlQuestion.Question;
            }
            return textQuestion;
        }

        public static List<RelLabworkControlQuestion> LoadRelationsLabworkControlQuestion(int LabworkId)
        {
            List<RelLabworkControlQuestion> questions = null;
            using (CmDbEntities db = new CmDbEntities())
            {
                questions = (
                    from rlcq in db.RelLabworkControlQuestion
                    where rlcq.LabworkId == LabworkId
                    select rlcq).ToList();
            }
            return questions;
        }

        public static void SaveControlQuestionAnswer(int labworkId, FullControlQuestion fullControlQuestion)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                RelLabworkControlQuestionUser rlcqu = 
                    new RelLabworkControlQuestionUser
                        {
                            LabworkId = labworkId,
                            ControlQuestionId = fullControlQuestion.QuestionId,
                            UserId = MyMembershipProvider.GetCurrentUserInstance().Id,
                            Answer = fullControlQuestion.AnswerText
                        };

                db.AddToRelLabworkControlQuestionUser(rlcqu);
                db.SaveChanges();
            }
        }
    }
}